/*
 * SaveTread.java
 *
 * Created on Pirmadienis, 2006, Lapkri�io 6, 11.38
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */


package keitykla;

import java.io.*;
import javax.microedition.io.*;
import java.util.*;
import j2mefs.*;

public class SaveTread
{
    private static boolean threadStart=false;
    private static SaveThread_ tred;
    
//-----------------------------------------------------------------------------    
    
    public static void download() 
    {
        if(threadStart)                                //Leidziam tik viena gija
            return ;
        SaveThread_ t = get();
        t.start();
        threadStart=true;
    }
    
    public static void stop()
    {
        SaveThread_ t = get();
        t.notify();
    }
    
    
    public static SaveThread_ get()
    {
        if(tred == null)
            tred = new SaveTread.SaveThread_();
        return tred;
    }
    
//-----------------------------------------------------------------------------    
    
    private static class SaveThread_ extends Thread 
    {

        public SaveThread_() 
        {
        }
        
//-----------------------------------------------------------------------------        
        
        public void run() 
        {

            try
            {
                Screens.showWaitting();
                wait();
            }
            catch (Exception ioe)
            {
                ioe.printStackTrace();
            } 
            finally 
            {
                threadStart=false;
            }
        }
        
//-----------------------------------------------------------------------------        
        
         void saveHashtable(Hashtable table) 
         {
            String all_data = null;
            Enumeration e=table.keys();
            System.out.println("  "+table.size());
            Currency cur = null;
            File file = null;
            ByteArrayOutputStream data = new ByteArrayOutputStream();
            for(int i = 0; i<table.size(); i++)
            {
                String code = (String)e.nextElement();
                cur = (Currency)table.get(code); 
                try
                {   
                    all_data = all_data+cur.getCode()+","+cur.getName()+","+ cur.getRate()+","+cur.getQuantity()+","+cur.getChanged()+","+cur.getDate()+"|";
                    data.write(cur.getBytes());
                    file = Application.createOrLoadFile(Application.getCurrenciesDirectory(),cur.getCode());
                    Application.writeFile(file,data);
                    data.reset(); 
                    
                }
                catch(Exception ee)
                {
                    System.out.println("nuluzau saugodamas ant atskiro tredo");
                    ee.printStackTrace();
                }
            }
            try
            {  
               data.reset();
               data.write(all_data.getBytes());
               file = Application.createOrLoadFile(Application.getCurrenciesDirectory(), "Sarasas");
               Application.writeFile(file, data);
            }
            catch(Exception ex)
            {
                System.out.println("nuluzau saugodamas dideli stringa !!");
                ex.printStackTrace();
            }
            System.out.println("baigiau saugoti i atminti....");
        }
    }
}

